/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.model.dao;

import hibernate.pojo.Binhluan;
import hibernate.util.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author phathuynh
 */
public class BinhLuanDAO {
    
    //<editor-fold defaultstate="collapsed" desc="1. Add comment">
    public static boolean addComment(Binhluan comment) {
        boolean flag = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(comment);
            transaction.commit();
            flag = true;
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
        } finally {
            session.close();
        }
        return flag;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="2. Lấy danh sách bình luận ứng với sản phẩm">
    public static List<Binhluan> DanhSachBinhLuanUngVoiSanPham(int maSanPham)
    {
        List<Binhluan> lstBinhLuan = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try
        {
            String hql = "from Binhluan bl where bl.sanpham.maSanPham=:maSanPham";
            Query query = session.createQuery(hql);
            query.setInteger("maSanPham", maSanPham);
            lstBinhLuan = query.list();
        }catch(HibernateException ex)
        {
            System.out.println(ex);
        }
        return lstBinhLuan;
    }
    //</editor-fold>
}
